package ch.elca.training.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.ListableBeanFactory;

import ch.elca.training.dao.ProjectDao;
import ch.elca.training.dom.Project;
import ch.elca.training.dom.ProjectQuery;
import ch.elca.training.service.ProjectService;
import ch.elca.training.service.common.BaseService;

public class ProjectServiceImpl extends BaseService implements ProjectService,
		BeanFactoryAware {

	private ListableBeanFactory m_beanFactory;
	private ProjectDao m_projectDao;
	private List<Project> m_allProjects;

	/** Get all projects */
	@SuppressWarnings("unchecked")
	public List<Project> getAllProjects() {
		if (m_allProjects == null) {
			Map<String, Project> projectsMap = m_beanFactory
					.getBeansOfType(Project.class);
			m_allProjects = new ArrayList<Project>(projectsMap.values());
		}

		return m_allProjects;
	}

	/**
	 * {@inheritDoc}
	 */
	public Project findById(long pid) {
		for (Project project : getAllProjects()) {
			if (project.getId() == pid) {
				return project;
			}
		}

		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		assert (beanFactory instanceof ListableBeanFactory) : "The bean factory must be a ListableBeanFactory";
		m_beanFactory = (ListableBeanFactory) beanFactory;
	}

	/**
	 * {@inheritDoc}
	 */
	public List<Project> findByQuery(ProjectQuery query) {
		List<Project> projects = new ArrayList<Project>();
		for (Project project : getAllProjects()) {
			String criteria = StringUtils.trimToEmpty(query.getSearchNumber());
			if (StringUtils.containsIgnoreCase("" + project.getProjectNumber(),
					criteria)) {
				projects.add(project);
			}
			criteria = StringUtils.trimToEmpty(query.getSearchName());
			if (StringUtils.containsIgnoreCase(project.getName(), criteria)) {
				projects.add(project);
			}
			criteria = StringUtils.trimToEmpty(query.getSearchCustomer());
			if (StringUtils.containsIgnoreCase(project.getCustomer(), criteria)) {
				projects.add(project);
			}
			criteria = StringUtils.trimToEmpty(query.getSearchStatus());
			if (StringUtils.containsIgnoreCase("" + project.getStatus(),
					criteria)) {
				projects.add(project);
			}
		}

		return projects;
	}

	public void deleteProject(Project project) throws Exception {
		// TODO Auto-generated method stub

	}

	public void editProject(Project project) throws Exception {
		// TODO Auto-generated method stub

	}

	public void saveProject(Project project) throws Exception {
		ProjectDao projectDao = getProjectDao();
		projectDao.saveOrUpdate(project);
	}

	/**
	 * @param projectDao
	 *            the projectDao to set
	 */
	public void setProjectDao(ProjectDao projectDao) {
		this.m_projectDao = projectDao;
	}

	/**
	 * @return the projectDao
	 */
	public ProjectDao getProjectDao() {
		return m_projectDao;
	}

	/**
	 * @param allProjects
	 *            the allProjects to set
	 */
	public void setAllProjects(List<Project> allProjects) {
		this.m_allProjects = allProjects;
	}
}
